package com.itheima.doudizhu3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class PokerGame {
    //牌盒
    static ArrayList<String> list = new ArrayList<>();
    //创建一个集合用于添加牌得价值
    static HashMap<String,Integer> hm = new HashMap<>();
    //准备牌
    static {
        String[] color = {"♦", "♣", "♥", "♠"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        for (String c : color) {
            for (String n : number) {
                list.add(c+n);
            }
        }

        list.add(" 小王");
        list.add(" 大王");

        //指定牌得价值
        //牌上得数字到map集合中判断是否存在
        //存在，获取价值
        //不存在，本身得数字就是价值
        hm.put("J",11);
        hm.put("Q",12);
        hm.put("K",13);
        hm.put("A",14);
        hm.put("2",15);
        hm.put("小王",16);
        hm.put("大王",17);
    }

    /*位置错误：您直接在类体中调用 hm.put()，而没有将其放在
    方法、构造函数或初始化块中。在 Java 中，不能在类体中直接执行方法调用。
    hm.put();*/

    public PokerGame(){
        //洗牌
        Collections.shuffle(list);

        //发牌
        //定义四个用于发牌得对象
        ArrayList<String> lord = new ArrayList<>();
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i<=2){
                lord.add(list.get(i));
                continue;
            }

            if (i%3==0){
                player1.add(list.get(i));
            }else if(i%3==1){
                player2.add(list.get(i));
            }else {
                player3.add(list.get(i));
            }
        }

        //对各个对象手中得牌进行排序
        order(lord);
        order(player1);
        order(player2);
        order(player3);

        lookPoker("底牌",lord);
        lookPoker("张三",player1);
        lookPoker("李四",player2);
        lookPoker("王五",player3);

    }

    private void lookPoker(String name, ArrayList<String> poker) {
        System.out.println(name);
        for (String p : poker) {
            System.out.print(p+" ");
        }
        System.out.println();
    }

    public void order(ArrayList<String> player){
        Collections.sort(player, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //先按照各个牌得价值进行排序，如果价值相同则按照牌得花色进行排序
                //获取牌的花色及价值
                String color1 = o1.substring(0, 1);
                int value1=getValue(o1);
                String color2 = o2.substring(0,1);
                int value2 = getValue(o2);
                int i = value1 - value2;
                return i==0?color1.compareTo(color2):i;
            }
        });
    }

    private int getValue(String poker) {
        String number = poker.substring(1);
        //判断截取部分在集合中是否存在
        if(hm.containsKey(number)){
            //存在
            return hm.get(number);
        }else{
            //不存在，该截取部分的本身就是价值
            return Integer.parseInt(number);
        }
    }

}
